<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>enum</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C/C++ Keywords</a> &gt; <a href="enum.html">enum</a>
  </div>

  <div class="name-format">
    enum
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  enum name {name-list} var-list;
</pre>

  <p>The enum keyword is used to create an enumerated type named name
  that consists of the elements in <em>name-list</em>. The
  <em>var-list</em> argument is optional, and can be used to create
  instances of the type along with the declaration.  For example, the
  following code creates an enumerated type for colors:</p>

  <div class="related-examples">
    <pre class="example-code">
   enum ColorT {red, orange, yellow, green, blue, indigo, violet};
   ...
   ColorT c1 = indigo;
   if( c1 == indigo ) {
     cout &lt;&lt; &quot;c1 is indigo&quot; &lt;&lt; endl;
   }            
</pre>
</div>

  <p>In the above example, the effect of the enumeration is to
  introduce several new constants named <em>red</em>, <em>orange</em>,
  <em>yellow</em>, etc.  By default, these constants are assigned
  consecutive integer values starting at zero.  You can change the
  values of those constants, as shown by the next example:</p>

  <div class="related-examples">
    <pre class="example-code">
   enum ColorT { red = 10, blue = 15, green };
   ...
   ColorT c = green;
   cout << "c is " << c << endl;
</pre>
</div>

  <p>When executed, the above code will display the following
  output:</p>

  <div class="related-examples">
    <pre class="example-code">
   c is 16
</pre>
</div>

  <p>Note that the above examples will only work with C++ compilers.
  If you're working in regular C, you will need to specify the
  <em>enum</em> keyword whenever you create an instance of an
  enumerated type:

  <div class="related-examples">
    <pre class="example-code">
   enum ColorT { red = 10, blue = 15, green };
   ...
   enum ColorT c = green;   // note the aditional enum keyword
   printf( "c is %d\n", c );
</pre>
</div>

  </td>
  


  </tr>
  </table>
</body></html>
